Lecture 04

Statistics 422

Design Principles

R Shiny

What is Shiny?

  • R Shiny is used for creating interactive web applications in R. Your users can change what they see by interacting with Shiny.

  • It isn’t really for us, it’s for others to interactively explore our data. Our users do not need to know R.

  • We can also use R Shiny to analyze data and create graphics and summaries - but the point is it is to be shared with others on the internet.

  • If your data updates, Shiny can automatically update the data. This means users always see the most current information.

  • As the designers of an R Shiny app, we design it to allow other uses to select predefined options or filters. We can design a Shiny app to look and behave exactly how we want!

How does Shiny work?

  • We use R to write the code for what the Shiny app should do with the data, e.g. compute summary stats, draw graphs, etc.
  • We also use R to design what the app will look like example — where the buttons, graphs, and sliders will be placed on a screen.
  • The R Shiny function converts your code into a web app that can be accessed through a browser

Creation

In RStudio

Select File-New File-Shiny Web App

Set Up - choices

Choose a name, choose a single file (app.R when just starting out), choose a location on your local drive to store your app

In RStudio

There should now be a new folder with an RStudio created demo file in it. Click on “Run App” in the source windown

In RStudio

If you run the app (green triangle icon on the upper right) the default Old Faithful Shiny app should pop up

In RStudio

You can open it in your browser to get a better look

Steps/Logic of App.R

  • We call up the library(Shiny)
  • and start with the construction of a user interface UI
  • the first step is to create a top level page layout

Steps/Logic of App.R (cont’d)

  • Shiny apps are hierarchical and logical
  • In their demo, fluidPage() function is used to create a fluid layout for Shiny web applications.
  • With fluid layout the page will automatically adjust its layout to fit the size of the user’s browser window.
  • This makes the user interface responsive and adaptable to different screen sizes making the Shiny app more appealing

Steps/Logic of App.R (cont’d)

  • Inside the page layout, panels are used to place controls and content
  • Here we have a sidebar layout whichs divides the screen into a main panel (output) and a sidebar panel (input)

Steps/Logic of App.R (cont’d)

  • Here we see the server side of the Shiny app
  • This is where the computations, data manipulation, and reactive connections between the UI inputs and server outputs happen

Please team up

Taking it all apart

  • I have an example file that does nothing. It is named Shiny0_proof.R
  • It exists to give you the absolute minimum that is needed to make Shiny run

In RStudio

  • The blank window is correct, now we will expand on it.

Commands

Websites

Cheatsheet for Shiny

  • In “Building an App” it lays out the structure for you.

UI

UI Input Choices

UI Closeup (shiny1_ui_only.R)

  • titlePanel() and sliderInput() notice the comma just before h1()

UI Closeup (shiny1_ui_only.R) (cont’d)

  • various text control

UI Closeup Result

  • Rendered Result

UI Closeup with image and bullets

UI Closeup with image and bullets result

The UI is an HTML document

## ### Remainder of shiny1_ui_only.R

At this point

  • There is no data, moving the slider just moves the slider
  • There is user input being collected, but the input isn’t being processed
  • The ui ends with the close parenthesis (note no comma)
  • We have not given instructions to the server for output
  • shinyApp(ui = ui, server = server) runs and we have a Shiny app

Server

Build Server side with render & output

  • See shiny2-ui_server_output.R
  • The remainder of Shiny is rendering input then results are output.

UI modified

  • In shiny2-ui_server_output.R, user input will be passed to the server and rendered.
  • UI changes a bit with some output functions - plotOutput(), textOutput(), DTOutput() and tableOutput()
  • The output functions will be connected to render functions

A closer look

  • There are four render-output pairs,
  • the rendering occurs in server
  • the first of the render functions here has a variable passed to it from ui:

Notes on user input and render

  • Within the {} in renderPlot() is the R code that will build a histogram
  • Only R code in the server portion, notice, just hist() from base R no html etc.
  • Access the input values with input$
  • The “num” in input$num is the value assigned to inputID in the sliderInput of the UI

render-output notes

  • server is a function then built using render functions,
  • the output contains values which are passed back to UI
  • Some values could originate from the server side

Result

Result

Ready to Publish?

  • Not that I would publish this, but I could.
  • Publishing options, see publishing Shiny
  • You can also have users download your app.R
  • ex. runGist("8ad7b367f0d4d9947a94c5387900b2bd") make sure your Shiny app has a standard name like app.R and not my non-standard ones.

Example

  • I have access to earthquake data in Southern California

  • I can create a Shiny app so my users could select a specific time frame and map the earthquakes.

  • They might adjust their choices to see data from different years, or click on the map to get different information about a particular are.